//
//  UIView+LLAdd.m
//  Receipt
//
//  Created by admin on 2019/6/27.
//  Copyright © 2019 admin. All rights reserved.
//

#import "UIView+LLAdd.h"

@implementation UIView (LLAdd)


- (void)addGradientRampWithColors:(NSArray<UIColor *> *)colors colorDirection:(GradientDirection)direction
{
    NSMutableArray *colorsArray = [NSMutableArray array];
    [colors enumerateObjectsUsingBlock:^(UIColor * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSAssert([obj isKindOfClass:[UIColor class]], @"数组里面的元素应该用UIColor类型");
        [colorsArray addObject:(__bridge id)obj.CGColor];
    }];
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.bounds;
//    NSLog(@"%@", NSStringFromCGRect(gradientLayer.frame));
    [gradientLayer setColors:colorsArray];
    
    [gradientLayer setStartPoint:CGPointMake(0, 0)];
    if (direction == VerticalDirection) {
        [gradientLayer setEndPoint:CGPointMake(0, 1)];
    } else {
        [gradientLayer setEndPoint:CGPointMake(1, 0)];
    }
    
//    [self.layer addSublayer:gradientLayer];
    [self.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)showInView:(nullable UIView *)inView animationDuration:(CGFloat)duration
{
    if (inView == nil) {
        inView = [UIApplication sharedApplication].keyWindow;
    }
    
    UIView *bgView = [[UIView alloc] initWithFrame:inView.bounds];
    bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    [inView addSubview:bgView];
    
    self.transform = CGAffineTransformMakeScale(0.1, 0.1);
    self.alpha = 0;
    self.center = bgView.center;
    [bgView addSubview:self];
    
    duration = duration == 0 ? 0.3 : duration;
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 1;
        bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)dismissWithAnimationDuration:(CGFloat)duration
{
    UIView *spView = self.superview;
    
    duration = duration == 0 ? 0.3 : duration;
    [UIView animateWithDuration:duration animations:^{
        
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
        spView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        
    } completion:^(BOOL finished) {
        
        [spView removeFromSuperview];
        
    }];
        
}



@end
